package com.wzk.canalsync.task;
import com.wzk.canalsync.utils.FileUtils;
import com.wzk.canalsync.utils.LogToFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.nio.file.*;
import java.util.logging.Logger;
/**
 * @author wangzikang
 */
//清除日志
@Slf4j
@Component
public class ClearLogsFileTask {

    public static final Logger logger = Logger.getLogger(ClearLogsFileTask.class.getName());

    //每8天执行一次
    @Scheduled(cron = "0 0 0 1/8 * *")
    public void clearLogs(){
        log.info("开始清理日志.....");
        try {
            Path logFilePath = Paths.get("logs");
            // 检查目录是否存在并且是一个目录
            if (Files.exists(logFilePath) && Files.isDirectory(logFilePath)) {
                //执行删除操作
                FileUtils.deleteDirectoryContents(logFilePath);
                log.info("清除日志文件成功！");
            } else {
                log.info("logs目录不存在！无需清除");
            }
        }catch (Exception e){
            log.error("定时删除日志文件出现异常！异常：{}",e.getMessage());
            LogToFile.logToFile("定时删除日志文件出现异常！异常："+e.getMessage(),logger,"clear_log_err");
        }
    }


}
